package com.pv.bfc.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.pv.bfc.dao.UserRepository;
import com.pv.bfc.model.UserSettings;
import com.pv.bfc.utils.GetUserDetailsFromContext;

@SuppressWarnings("deprecation")
public class UserSettingsController extends SimpleFormController {

	private static final Log logger = LogFactory
			.getLog(CreateFolderController.class);

	private UserRepository userRepo;

	private GetUserDetailsFromContext usrDtls;

	public UserSettingsController() {
		setCommandClass(UserSettings.class);
		setCommandName("usrProfile");
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		UserSettings usrProf = null;

		// Getting User Details
		User principal = (User) usrDtls.getPrincipal();

		String userName = principal.getUsername();

		logger.info("UserName in side UserSettingsController - " + userName);

		if (userName != null && !"".equals(userName)) {
			usrProf = userRepo.getUserProfile(userName);
			return usrProf;
		} else {
			return super.formBackingObject(request);
		}
	}

	@Override
	protected void doSubmitAction(Object folder) throws Exception {
		logger.info("IN UserSettingsController - doSubmitAction");
		// Write here logic for User Info Update - Stored Procedure

		logger.info("OUT UserSettingsController - doSubmitAction");
	}

	public void setUserRepo(UserRepository userRepo) {
		this.userRepo = userRepo;
	}

	public void setusrDtls(GetUserDetailsFromContext usrDtls) {
		this.usrDtls = usrDtls;
	}
}
